package com.example.reagent_management.ui.statistics

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.viewModels
import com.example.lib_base.base.BaseDBFragment
import com.example.lib_base.base.DataBindingConfig
import com.example.reagent_management.R
import com.example.reagent_management.databinding.FragmentStatisticsPositionBinding
import com.example.reagent_management.ui.statistics.helper.MyMarkerView
import com.example.reagent_management.ui.statistics.helper.initLineChart
import com.example.reagent_management.ui.statistics.helper.refreshLineData
import com.example.reagent_management.ui.statistics.pojo.OperateTimesAndNum
import com.github.mikephil.charting.data.Entry

private const val TITLE_KEY = "com.dragon.long.title"

class LineChartFragment : BaseDBFragment() {

    //从父Fragment中拿到共享ViewModel
    private val statistics: StatisticsViewModel by viewModels({ requireParentFragment() })

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.fragment_statistics_position)
    }


    @SuppressLint("SetTextI18n")
    override fun initViewOrObserve() {
        val type = arguments?.getInt(TITLE_KEY, 1)
        with(binding as FragmentStatisticsPositionBinding) {
            initLineChart(inStoreChart)
            initLineChart(outStoreChart)
            //设置
            inStoreChart.marker = MyMarkerView(this@LineChartFragment.requireContext()).apply { chartView = inStoreChart }
            outStoreChart.marker = MyMarkerView(this@LineChartFragment.requireContext()).apply { chartView = outStoreChart }
            val inVls = ArrayList<Entry>()
            val outVls = ArrayList<Entry>()
            val xVls = ArrayList<String>()
            when (type) {
                1 -> {
                    //近24H
                    statistics.getHourStore()
                    statistics.hourOutStore.observe(viewLifecycleOwner) {
                        setYValues(outVls, it)
                        if (outVls.isNotEmpty())
                            refreshLineData(outStoreChart, outVls, false, false)
                        outStoreChart.invalidate()
                    }
                    statistics.hourInStore.observe(viewLifecycleOwner) {
                        setYValues(inVls, it)
                        if (inVls.isNotEmpty())
                            refreshLineData(inStoreChart, inVls, true, false)
                        inStoreChart.invalidate()
                    }
                }
                2 -> {
                    //近7天
                    statistics.getWeekStore()
                    statistics.weekInStore.observe(viewLifecycleOwner) {
                        setYValues(inVls, it)
                        if (inVls.isNotEmpty())
                            refreshLineData(inStoreChart, inVls, true, true)
                        inStoreChart.invalidate()
                    }
                    statistics.weekOutStore.observe(viewLifecycleOwner) {
                        setYValues(outVls, it)
                        if (outVls.isNotEmpty())
                            refreshLineData(outStoreChart, outVls, false, true)
                        outStoreChart.invalidate()
                    }
                }
                else -> {}
            }
            operateType.setOnCheckedChangeListener { _, checkedId ->
                this.motionLayout.clearAnimation()
                if (checkedId == R.id.outStore){
                    this.motionLayout.transitionToState(R.id.end)
                }else if (checkedId == R.id.inStore){
                    this.motionLayout.transitionToState(R.id.start)
                }
            }
        }
    }

    private fun setYValues(list: ArrayList<Entry>,dataSource:List<OperateTimesAndNum>){
        list.clear()
        dataSource.forEachIndexed { _, operateTimesAndNum ->
            Log.e(TAG, "Store: $operateTimesAndNum" )
            list.add(Entry(operateTimesAndNum.operateTime.toFloat(),operateTimesAndNum.num))
        }
    }

    override fun initViewModel() {

    }

    companion object {
        //创建fragment碎片，但布局一样，内容不一样,当然也可拆出来，代码也就可以拆出一部分了
        fun create(id: Int) =
            LineChartFragment().apply {
                arguments = Bundle(1).apply {
                    putInt(TITLE_KEY, id)
                }
            }
        const val TAG = "LineChartFragment"
    }
}